package com.buss.base.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.base.dao.BussinessBillNoDao;
import com.buss.base.model.BussinessBillNo;
import com.buss.core.dic.BussinessBillNoType;
import com.buss.core.persistence.BaseDao;

@Repository
public class BussinessBillNoDaoImpl implements BussinessBillNoDao {

	@Autowired
	private BaseDao<BussinessBillNo> baseDao;
	
	@Override
	public String genBillNoVal(String prefix, String companyId, BussinessBillNoType billNoType) {
		String sql = "select billNo from BussinessBillNo billNo where billNo.companyId = :companyId " +
				"and billNo.billType = :billType";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("billType", billNoType);
		BussinessBillNo billNo = baseDao.loadHsql(sql, params);
		if(billNo == null){
			billNo = new BussinessBillNo();
			billNo.setCompanyId(companyId);
			billNo.setBillType(billNoType);
			billNo.setVal(1l);
		}else{
			billNo.setVal(billNo.getVal() + 1);
		}
		baseDao.saveOrEdit(billNo);
		String no = String.format("%08d", billNo.getVal()); 
		no = prefix + no;
		return no;
	}

}
